package zozopo.background.operator;

import java.io.IOException;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorOS;
import zozopo.background.operator.operations.OperatorTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class ModifyPasswordAction extends ActionSupport implements Action,SessionAware{
	//传入参数
	private String password;
	private String newpassword;
	private String comfirmnewpassword;
	
	//传出参数
	private String operationResult;
	
	//
	private Map<Object, Object> session;
	
	public String execute(){
		OperatorOS operatorOS = new OperatorOS();
		
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 

		if(operator == null){
			return NONE;
		}

		try {
			if(operatorOS.isRealOperator(operator.getEmail_operator(), password)){
				operatorOS.updatePassowrdByEmial(operator.getEmail_operator(), newpassword);
				operationResult = "修改成功"; 
			}else{
				operationResult = "输入密码错误!";
			}
			
		 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			operatorOS.releaseConnection();
		}

		
		return SUCCESS;
	}
	
	
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	
	
	public void validate() {
		//权限处理

		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 

		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		if(!newpassword.equals(comfirmnewpassword)){
			
			addFieldError("errormessage","输入两次密码不一致!");
			return ; 
		}
	}

	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public String getNewpassword() {
		return newpassword;
	}


	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}


	public String getComfirmnewpassword() {
		return comfirmnewpassword;
	}


	public void setComfirmnewpassword(String comfirmnewpassword) {
		this.comfirmnewpassword = comfirmnewpassword;
	}
	
	public String getOperationResult() {
		return operationResult;
	}


	public void setOperationResult(String operationResult) {
		this.operationResult = operationResult;
	}
}
